<?php

namespace app\system\controller;
use app\common\base\_Db;
use app\common\controller\CommonController;
use app\common\entity\RequestEntity;
use think\Db;

class Externalcompany extends CommonController{
    public function GetUserName($id){
        $res = Db::name('staff')
            ->field('nick_name')
            ->where('staff_id ='.$id)
            ->find();
        return $res['nick_name'];
    }
    public function AddCompany(){
        $CompanyName = new RequestEntity('CompanyName', '公司名称', false, 32);
        $post_creatid = new RequestEntity('CreatId', '操作人', false, 32);
        if (
        Db::name('external_company')
            ->insert(
                [
                    'company_name' => $CompanyName->text,
                ]
            )
        ) {
            _Db::log($post_creatid->text,1,'外部公司添加');
            $this->ListCompany();
        } else {
            _Db::Response(0,'添加失败');
        }
    }
    public function ListCompany(){
        $param_page = $get_page = input('Page', 1);
        $param_pagesize =  10;
        $get_keyword = new RequestEntity('search_key', '搜索内容', false, 0);
        $where = '1=1';
        if(!empty($get_keyword->text)){
            $where .= ' and company_name  like "' . $get_keyword->text.'%"';
        }
        $res = Db::name('external_company')
            ->field([
                'company_id',
                'company_name',
            ])
            ->where($where)
            ->limit(($param_page - 1) * $param_pagesize, $param_pagesize)
            ->select();
        foreach ( $res as $key => $value){
            $res[$key]['Order'] = $key + 1;
        };
        $int_total_count = count(Db::name('external_company')->where($where)->select());
        $data['tableData']=$res;
        $data['TotalCount'] = $int_total_count;
        $data['Page'] = (int)$param_page;
        $data['PageSize'] = (int)$param_pagesize;
        _Db::ResponseJson($data);
    }
    public function ItemCompany(){
        $CompanyId = new RequestEntity('CompanyId', '公司名称', false, 32);
        $res = Db::name('external_company')
            ->field('company_name,company_id')
            ->where('company_id ='.$CompanyId->text)
            ->find();
        _Db::ResponseJson($res);
    }
    public function ModifyCompany(){
        $CompanyId = new RequestEntity('CompanyId', '公司id', false, 32);
        $CompanyName = new RequestEntity('CompanyName', '公司名称', false, 32);
        $post_creatid = new RequestEntity('CreatId', '操作人', false, 32);
        if(
        Db::name('external_company')
            ->where('company_id ='.$CompanyId->text)
            ->update([
                'company_name'=>$CompanyName->text,
            ])
        ){
            _Db::log($post_creatid->text,3,'外部公司修改');
            $this->Item($CompanyId->text);
        }else{
            _Db::Response(0,'修改失败');
        }
    }
    public function Item($id){
        $res = Db::name('external_company')
            ->field('company_name,company_id')
            ->where('company_id ='.$id)
            ->find();
        _Db::ResponseJson($res);
    }
    public function RemoveCompany(){
        $CompanyId = new RequestEntity('CompanyId', '部门id', false, 32);
        $post_creatid = new RequestEntity('CreatId', '操作人', false, 32);
        $res = Db::name('external_company')->alias('e')
            ->field('s.nick_name')
            ->join('external_staff s','s.company_id = e.company_id')
            ->where('s.company_id ='.$CompanyId->text)
            ->select();
        if(!empty($res)){
            _Db::Response(3,'有员工不能删除');
            //return ResponseJson::Json(['code'=>1,'message'=>'有员工不能删除']);
        }else{
            Db::name('external_company')
                ->where('company_id ='.$CompanyId->text)
                ->delete();
            _Db::log($post_creatid->text,2,'外部公司删除');
            $this->ListCompany();
        }
    }
}